<?php
namespace app\admin\traits;
use think\Request;
trait Curd {
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $where = [];
        // 自定义搜索
        if(method_exists($this, 'searchConditionHandle')){
            $this->searchConditionHandle($where);
        }
        // 排序字段
        $sort_by = $this->request->param('sort/s', $this->model->getPk());
        // 升序/降序
        $desc = $this->request->param('descending/s', 'true');
        // 分页容量
        $page_size = $this->request->param('page_size/d',$this->page_size);
        if($page_size > 0) {
            // 回收站状态
            // if($this->request->param('trashed/s', 'false') === 'true'){
            //     $result = $this->model->onlyTrashed()->where($where)->order($desc === 'true' ? $sort_by.' desc' : $sort_by)->paginate($page_size);
            // }else{
            $result = $this->model->where($where)->order($desc === 'true' ? $sort_by.' desc' : $sort_by)->paginate($page_size);
            // }
        } else {
            // // 回收站状态
            // if($this->request->param('trashed/s', 'false') === 'true'){
            //     $result = $this->model->onlyTrashed()->where($where)->order($desc === 'true' ? $sort_by.' desc' : $sort_by)->select();
            // }else{
            $result = $this->model->where($where)->order($desc === 'true' ? $sort_by.' desc' : $sort_by)->select();
            // }
        }
        return json($result);
    }
    /**
     * 添加新数据
     *
     * @return void
     */
    public function save(Request $request){
        $data = $request->post();
        $data['author_id'] = $this->admin_id;
        if(!$this->validate->scene('create')->check($data)){
            return json($this->validate->getError(),500);
        }else {
            // $this->model->strict(false)->insert($data);
            $this->model->save($data);
            return json('新增成功！');
        }
    }
    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        if ($id === null) return json('参数错误！',500);
        $id = explode('_',$id);
        // if(is_string($id)) {
        //     $data = $this->model->where($this->model->getPk(),$id)->find();
        //     if(empty($data) || empty($id)){
        //         return json('未找到数据！',500);
        //     }
        //     $data->delete();
        //     return json('删除成功！');
        // } else if (is_array($id)) {
            $this->model->destroy($id);
            return json('删除成功！');
        // }
    }
    /**
     * 更新数据
     *
     * @return void
     */
    public function update(Request $request, $id) {
        $data = $request->post();
        unset($data['update_time']);unset($data['create_time']);
        if(!$this->validate->scene('update')->check($data)){
            return json($this->validate->getError(),500);
        }else {
            $this->model->where($this->model->getPk(),$id)->update($data);
            return json('更新成功！');
        }
    }
    /**
     * 获取单个数据
     *
     * @return void
     */
    public function read($id)
    {
        if(empty($id)){
            return json('错误的查询条件！',500);
        }
        $data = $this->model->where($this->model->getPk(),$id)->find();
        return json($data);
    }
    /**
     * 更新单一字段
     *
     * @return void
     */
    public function updatefield() {
        $this->setAllowMethod(['put']);
        $primary_key = $this->request->param('id',null);
        $field = $this->request->param('field',null);
        $value = $this->request->param('value',null);
        if(empty($primary_key) || empty($field) || empty($value)){
            return json('数据不完整',500);
        }else {
            $this->model->where($this->model->getPk(),$primary_key)->setField($field, $value);
            return json('数据更新成功！');
        }
    }
    /**
     * 批量更新
     *
     * @return void
     */
    public function batch(){
        $this->setAllowMethod(['put']);
        $selected_id_array = $this->request->param('selected_id/a',null);
        // var_dump($selected_id_array);exit;
        $field = $this->request->param('field',null);
        $value = $this->request->param('value',null);
        if(empty($selected_id_array) || empty($field) || empty($value)){
            return json('数据不完整',500);
        }else {
            // $selected_id_array = json_decode($selected_id, true);
            $count = $this->model->where($this->model->getPk(),'in',$selected_id_array)->setField($field, $value);
            return json('批量数据更新成功！共更新'.$count.'条数据');
        }
    }
    
    /**
     * 设置允许的方法，及错误返回
     *
     * @param [type] $method_array
     * @return void
     */
    protected function setAllowMethod($method_array)
    {
        $method = $this->request->method();
        if(!in_array(strtolower($method),$method_array)){
            json('错误的请求类型 Method Not Allowed',405)->send();
            die();
        }
    }
}